# Copyright (c) 2023, hzmes and contributors
# For license information, please see license.txt

import frappe
import json
from srm.srm.misinterface import MisApi
from frappe.model.document import Document
from frappe.model.workflow import apply_workflow

class SRMSupplierApprovalL2L3(Document):
	pass

	@frappe.whitelist()
	def sync_to_oa_workfolw(self):
		api = MisApi()
		owner = frappe.get_doc('User', self.owner)
		try:
			l = frappe.get_all('SRM Third API Config', filters={'disable': 0}, fields=['*'])
			formdatajson = {"ext": 
								{"bill_no": self.name,
									"request_user": self.applicant_by,
									"supplier_no": self.supplier_number,
									"supplier_name": self.supplier_name,
									"legal_person": self.legalperson,
									"exec_type": self.type,
									"create_date": self.company_start_time,
									"emp_qty": self.employees_count,
									"registered_capital": self.capital_ten_thousand,
									"credit_code": self.tax_identify,
									"address": self.default_address,
									"pay_type": self.srm_payment_mode,
									"company_type": self.company_nature,
									"brand": self.agent_brand,
									"settle_type": self.settlestyle,
									"contact": self.full_name,
									"telephone": self.phone,
									"email": self.email_address,
									"value_remark": self.benefit_statement,
									"intro_remark": self.supplier_import_description,
									"materiel_type": self.supply_material_category,
									"review_log": f"复核人: {self.reviewe_by} 审核人: {self.checke_by}",
									"company_profile": self.company_introduction if self.company_introduction else '',
									# "company_profile": '<a href="https://www.baidu.com/">公司简介附件下载链接</a>'
									}
							}
			# 保存
			saveReturnData = api.bill_save({
						# 2023-07-27调整: OA系统中的创建人(工号)不是配置中固定, 调整为self.owner对应的工号
						"billUserNo": owner.location.strip(),
						"businessDataJSON": "{}",
						"callbackUrl": "",
						"flowableBusinessId": l[0].flowablebusinessid,
						"formDataJSON": json.dumps(formdatajson),
						"formUrl": ""
						})
			self.oa_billno = saveReturnData
			self.save()
			frappe.db.commit()
			return formdatajson
		except Exception as e:
			print(e)

	@frappe.whitelist()
	def sync_del_oa_workfolw(self, query_billId):
		api = MisApi()
		try:
			cakkBackReturnData = api.call_back({
					"billId": query_billId,
					# "billId": "1650684367552385025",
				})
			self.oa_billno = None
			self.save()
			# self.reload()
		except Exception as e:
			print(e)

	@frappe.whitelist()
	def sync_query_oa_workfolw(self, query_billId):
		api = MisApi()
		try:
			queryReturnData = api.bill_query({"billId": query_billId})
			msg = ''
			auditusername = ''
			for i in queryReturnData[0]["commentList"]:
				if i["commentTypeName"] != '流程审批结束' and i["commentTypeName"] != '发起申请':
					msg = msg + i["commentTypeName"] + "/" + i["commentContent"] + "/" + i["taskName"] + "/" + i["auditUserName"] + ","
					auditusername = i["auditUserName"]
			if queryReturnData[0]["billStatus"] == 1:
				self.oa_billnum = queryReturnData[0]["billNum"]
				self.oa_comment = msg
				self.save()
			if queryReturnData[0]["billStatus"] == 90:
				self.oa_billnum = queryReturnData[0]["billNum"]
				self.oa_comment = msg
				apply_workflow(self, 'Reject')
				self.reject_by = auditusername
				self.save()
			if queryReturnData[0]["billStatus"] == 2:
				self.oa_billnum = queryReturnData[0]["billNum"]
				self.oa_comment = msg
				apply_workflow(self, 'Approve')
				self.approve_by = auditusername
				self.save()
			return queryReturnData
			# return auditusername
		except Exception as e:
			print(e)

	# 2023-07-27: 只是用来查询OA的审批进度不修改当前Doc
	@frappe.whitelist()
	def query_oa_workfolw(self, query_billId):
		api = MisApi()
		try:
			queryReturnData = api.bill_query({"billId": query_billId})
			return queryReturnData
		except Exception as e:
			print(e)
